<?php
//COLORLIB!!!!
//Haven't test each one of the functions yet
//so use with care...
class colorlib{
		
		
	static function rgb2hex($color){
		$i = 0;
		while($i<3){
			$color[$i] = dechex(min(max(0,$color[$i]),255));
			++$i;
		}
	       
	       return implode("", $color);
	   }

	static function hex2rgb($color){
	    if ($color[0] == '#')
	        $color = substr($color, 1);
	    switch (strlen($color)){
	    case 8:
	        list($r, $g, $b, $a) = array($color[0].$color[1],
	                                 $color[2].$color[3],
	                                 $color[4].$color[5],$color[6].$color[7]);
	    break;
	    case 6:
	        list($r, $g, $b) = array($color[0].$color[1],
	                                 $color[2].$color[3],
	                                 $color[4].$color[5]);
	    break;
	    case 4:
	        list($r, $g, $b, $a) = array($color[0], $color[1], $color[2], $color[3]);
	    break;
	    case 3:
	        list($r, $g, $b) = array($color[0], $color[1], $color[2]);
	    break;
	    default:
	    	return false;
	    break;
	    }
	    //Now, the color will not be invalid
	    $r = max(min(hexdec($r),255),0);
	    $g = max(min(hexdec($g),255),0);
	    $b = max(min(hexdec($b),255),0);
	    if(isset($a)){
			return array($r, $g, $b, max(min(hexdec($a),127),0));
		}
	    return array($r, $g, $b);
	}

	function rgb2hsl($color){
		$r = $color[0]/256;
		$g = $color[1]/256;
		$b = $color[2]/256;
		if(($r===$g)&&($g===$b)){
			$h = 0;
			$s = 0;
			$l = $r;
		}else{
			$max = max($r, $g, $b);
			$min = min($r, $g, $b);
			$l = ($min + $max)/2;
			if($l < 0.5){
				$s = ($max-$min)/($max+$min);
			}else{
				$s = ($max-$min)/(2-$max-$min);
			}
			
			if($r === $max){
				$h = ($g -$b)/($max - $min);
			}elseif($g === $max){
				$h = 2 + ($b - $r)/($max-$min);
			}else{
				$h = 4 + ($r - $g)/($max-$min);
			}
			$h /= 6;
			if($h < 0){
				++$h;
			}
		}
		return array(round($h*255), round($s*255), round($l*255));
	}

	function cmy2rgb($color){
		return array((1-$color[0])*255,(1-$color[1])*255,(1-$color[2])*255);
	}

	function rgb2cmy($color){
		if ($color[0]==0) $color[0] = 1;
		if ($color[1]==0) $color[1] = 1;
		if ($color[2]==0) $color[2] = 1;
		return array( 1- ($color[0]/255), 1 - ($color[1]/255), 1 - ($color[2]/255));
	}

	function cmy2cmyk($color){
		$k = 1;
		$k = max($color);
		if ( $k == 1 ) { //Black
		   $color[0] = 0;
		   $color[1] = 0;
		   $color[2] = 0;
		}
		else {
		   $color[0] = ($color[0] - $k) / (1 - $k);
		   $color[1] = ($color[1] - $k) / (1 - $k);
		   $color[2] = ($color[2] - $k) / (1 - $k);
		}
		return array($color[0],$color[1],$color[2],$k);
	}

	function cmyk2cmy($color){
		return array(($color[0] * ( 1 - $color[3] ) + $color[3]),($color[1] * ( 1 - $color[3] ) + $color[3]),($color[2] * ( 1 - $color[3] ) + $color[3]));
	}
}

function imarty_color($image, $color, $alpha = 0){
	$color = colorlib::hex2rgb((string)$color);
	//If transpriancy is zero, then we use imagecolorallocate
	if($color[3]){
		return imagecolorallocatealpha($image, $color[0],$color[1],$color[2],$color[3]);
	}
	//If transpriancy is zero, then we use imagecolorallocate
	if($alpha){
		return imagecolorallocatealpha($image, $color[0],$color[1],$color[2],$alpha);
	}
	return imagecolorallocate($image, $color[0],$color[1],$color[2]);
}

function imagecreatetransparent($w, $h){
	$image = imagecreatetruecolor($w, $h);
		        $b = imagecreatefromstring(base64_decode('iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAADqSURBVHjaYvz//z/DYAYAAcTEMMgBQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAgAEAMpcDTTQWJVEAAAAASUVORK5CYII='));
	imagealphablending($image,false);
	imagesavealpha($image,true);
		        imagecopyresized($image,$b,0,0,0,0,$w,$h,imagesx($b),imagesy($b));
	return $image;
}
?>